<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class class_service{
	private $CI;
	function __construct() { 
		$this->CI = &get_instance();
		$this->CI->load->model('Class_model');
	}

	/**  
	* 获取课程，满足日历格式要求
	* @access public 
	* @param  $date 日期 年月日 默认不传 false  表示所有
	* @return array
	*/  
	public function getClass($date=false,$type=false) {
		$connar['where']=" 1=1 ";
		if($date){
			// $connar['where'].=$this->CI->sqlEscape(" and YEARWEEK(date_format(from_unixtime(??, '%Y-%m-%d'),'%Y-%m-%d')) = YEARWEEK(from_unixtime(start_time, '%Y-%m-%d')) ",array($date));
			$connar['where'].=$this->CI->sqlEscape(" and from_unixtime(start_time, '%Y-%m')=from_unixtime(??, '%Y-%m') ",array($date));
		}
		$class=$this->CI->Class_model->get_list_full($connar);
		$new_class=array();
		foreach ($class as $k => $v) {
			$start_time_array=explode(' ',date('Y-m-d H:i:s',$v['start_time']));
			$end_time_array=explode(' ',date('Y-m-d H:i:s',$v['end_time']));
			$color=$v['color'];
			$new_class[$k]=array(
				'id'=>$v['id'],
				'title'=>$v['name'].' （'.$v['classroom'].'）',
				'start'=>$start_time_array[0].'T'.$start_time_array[1],
				'end'=>$end_time_array[0].'T'.$end_time_array[1],
				'color'=>$color,
				'allDay'=>false
				);
		}
		return $new_class;
	}

	/**  
	* 根据课程id 获取课程详细信息
	* @access public 
	* @param  $class_id 课程id
	* @return array
	*/  
	public function getClassById($class_id) {
		$connar['cond']=array('class.id'=>$class_id);
		$connar['join']=array('admin a','a.id=class.teacher_id');
		$connar['items']=" class.*,a.username ";
		$class=$this->CI->Class_model->get_single_full($connar);
		return $class;
	}

	
}